All files / web/src/app/api/arcade/rooms/[roomId]/deactivate-player route.ts

0% Statements 0/120
0% Branches 0/1
0% Functions 0/1
0% Lines 0/120

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121                                                                                                                                                                                                                                                 
import { NextResponse } from 'next/server'
import { getRoomMembers } from '@/lib/arcade/room-membership'
import { getPlayer, getRoomActivePlayers, setPlayerActiveStatus } from '@/lib/arcade/player-manager'
import { withAuth } from '@/lib/auth/withAuth'
import { getUserId } from '@/lib/viewer'
import { getSocketIO } from '@/lib/socket-io'

/**
 * POST /api/arcade/rooms/:roomId/deactivate-player
 * Deactivate a specific player in the room (host only)
 * Body:
 *   - playerId: string - The player to deactivate
 */
export const POST = withAuth(async (request, { params }) => {
  console.log('[Deactivate Player API] POST request received')

  try {
    const { roomId } = (await params) as { roomId: string }
    console.log('[Deactivate Player API] roomId:', roomId)

    const userId = await getUserId()
    console.log('[Deactivate Player API] userId:', userId)

    const body = await request.json()
    console.log('[Deactivate Player API] body:', body)

    // Validate required fields
    if (!body.playerId) {
      console.log('[Deactivate Player API] Missing playerId in body')
      return NextResponse.json({ error: 'Missing required field: playerId' }, { status: 400 })
    }

    // Check if user is the host
    console.log('[Deactivate Player API] Fetching room members for roomId:', roomId)
    const members = await getRoomMembers(roomId)
    console.log('[Deactivate Player API] members count:', members.length)

    const currentMember = members.find((m) => m.userId === userId)
    console.log('[Deactivate Player API] currentMember:', currentMember)

    if (!currentMember) {
      return NextResponse.json({ error: 'You are not in this room' }, { status: 403 })
    }

    if (!currentMember.isCreator) {
      return NextResponse.json({ error: 'Only the host can deactivate players' }, { status: 403 })
    }

    // Get the player
    console.log('[Deactivate Player API] Looking up player with ID:', body.playerId)
    const player = await getPlayer(body.playerId)
    console.log('[Deactivate Player API] Player found:', player)

    if (!player) {
      console.log('[Deactivate Player API] Player not found in database')
      return NextResponse.json({ error: 'Player not found' }, { status: 404 })
    }

    console.log('[Deactivate Player API] Player userId:', player.userId)
    console.log(
      '[Deactivate Player API] Room member userIds:',
      members.map((m) => m.userId)
    )

    // Can't deactivate your own players (use the regular player controls for that)
    if (player.userId === userId) {
      console.log('[Deactivate Player API] ERROR: Cannot deactivate your own players')
      return NextResponse.json(
        {
          error: 'Cannot deactivate your own players. Use the player controls in the nav.',
        },
        { status: 400 }
      )
    }

    // Note: We don't check if the player belongs to a current room member
    // because players from users who have left the room may still need to be cleaned up
    console.log('[Deactivate Player API] Player validation passed, proceeding with deactivation')

    // Deactivate the player
    await setPlayerActiveStatus(body.playerId, false)

    // Broadcast updates via socket
    const io = await getSocketIO()
    if (io) {
      try {
        // Get updated player list
        const memberPlayers = await getRoomActivePlayers(roomId)

        // Convert memberPlayers Map to object for JSON serialization
        const memberPlayersObj: Record<string, any[]> = {}
        for (const [uid, players] of memberPlayers.entries()) {
          memberPlayersObj[uid] = players
        }

        // Notify everyone in the room about the player update
        io.to(`room:${roomId}`).emit('player-deactivated', {
          roomId,
          playerId: body.playerId,
          playerName: player.name,
          deactivatedBy: currentMember.displayName,
          memberPlayers: memberPlayersObj,
        })

        console.log(
          `[Deactivate Player API] Player ${body.playerId} (${player.name}) deactivated by host in room ${roomId}`
        )
      } catch (socketError) {
        console.error('[Deactivate Player API] Failed to broadcast deactivation:', socketError)
      }
    }

    console.log('[Deactivate Player API] Success - returning 200')
    return NextResponse.json({ success: true }, { status: 200 })
  } catch (error: any) {
    console.error('[Deactivate Player API] ERROR:', error)
    console.error('[Deactivate Player API] Error stack:', error.stack)
    return NextResponse.json({ error: 'Failed to deactivate player' }, { status: 500 })
  }
})